package gov.tax.rti.base.util;

import java.io.StringWriter;
import java.util.Map;

import net.sf.cglib.beans.BeanMap;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.VelocityEngine;

import com.bstek.dorado.core.Context;
import com.bstek.dorado.view.resolver.VelocityHelper;

public abstract class SqlUtils {

	@SuppressWarnings("unchecked")
	public static String build(String sql, Object parameter) throws Exception {
		VelocityContext context = new VelocityContext();

		Map<Object, Object> map = null;
		if (parameter != null) {
			if (parameter instanceof Map) {
				map = (Map<Object, Object>) parameter;
			} else {
				map = BeanMap.create(parameter);
			}
		}

		if (map == null) {
			context = new VelocityContext();
		} else {
			context = new VelocityContext(map);
		}

		StringWriter result = new StringWriter(50);

		Context doradoContext = Context.getCurrent();
		VelocityHelper VelocityHelper = (VelocityHelper) doradoContext
				.getServiceBean("velocityHelper");
		VelocityEngine volocityEngine = VelocityHelper.getVelocityEngine();
		volocityEngine.evaluate(context, result, "", sql);
		return result.toString();
	}

}
